package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.StrategyCatalog;
import cn.wolfcode.trip.base.domain.StrategyContent;
import cn.wolfcode.trip.base.domain.StrategyDetail;
import cn.wolfcode.trip.base.mapper.StrategyContentMapper;
import cn.wolfcode.trip.base.mapper.StrategyDetailMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IStrategyDetailService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService{
    @Autowired
    private StrategyDetailMapper strategyDetailMapper;
    @Autowired
    private StrategyContentMapper contentMapper;

    @Override
    public void saveOrUpdate(StrategyDetail entity) {
        StrategyContent strategyContent = entity.getStrategyContent();
        if (entity.getState()) {
            entity.setReleaseTime(new Date());
        }
        if (entity.getSequence() == null) {
            int max = strategyDetailMapper.selectMaxSequence(entity.getCatalog().getId());
            entity.setSequence(max);
        }
        if (entity.getId() == null) {
            entity.setCreateTime(new Date());
            strategyDetailMapper.insert(entity);
            strategyContent.setId(entity.getId());
            contentMapper.insert(strategyContent);

        }else{
            strategyDetailMapper.updateByPrimaryKey(entity);
            strategyContent.setId(entity.getId());
            contentMapper.updateByPrimaryKey(strategyContent);

        }
    }

    @Override
    public PageInfo list(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize(),qo.getOrderBy());
        List<StrategyDetail> list = strategyDetailMapper.selectForList(qo);
        return new PageInfo<>(list);
        
    }

    @Override
    public StrategyDetail selectById(Long id) {
        return strategyDetailMapper.selectByPrimaryKey(id);
    }

    @Override
    public StrategyContent getContentById(Long detailId) {
        return contentMapper.selectByPrimaryKey(detailId);
    }


}
